package org.dromara.Nyt7PlotDetailInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfo;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.vo.Nyt7FarmerInsuranceInfoVo;
import org.dromara.Nyt7PlotDetailInfo.domain.Nyt7PlotDetailInfoEx;
import org.dromara.Nyt7PlotDetailInfo.domain.vo.Nyt7PlotDetailInfoExVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7PlotDetailInfo.domain.bo.Nyt7PlotDetailInfoBo;
import org.dromara.Nyt7PlotDetailInfo.domain.vo.Nyt7PlotDetailInfoVo;
import org.dromara.Nyt7PlotDetailInfo.domain.Nyt7PlotDetailInfo;
import org.dromara.Nyt7PlotDetailInfo.mapper.Nyt7PlotDetailInfoMapper;
import org.dromara.Nyt7PlotDetailInfo.service.INyt7PlotDetailInfoService;


import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 地块详细信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-27
 */
@RequiredArgsConstructor
@Service
public class Nyt7PlotDetailInfoServiceImpl implements INyt7PlotDetailInfoService {

    private final Nyt7PlotDetailInfoMapper baseMapper;

    /**
     * 查询地块详细信息
     *
     * @param id 主键
     * @return 地块详细信息
     */
    @Override
    public Nyt7PlotDetailInfoVo queryById(Long id){
        Nyt7PlotDetailInfoEx info = baseMapper.selectByIdEx(id);


        return baseMapper.selectVoById(id);
    }
    /**
     * 查询地块详细信息
     *
     * @param id 主键
     * @return 地块详细信息
     */
    @Override
    public Nyt7PlotDetailInfoExVo queryByIdEx(Long id){
        Nyt7PlotDetailInfoEx info = baseMapper.selectByIdEx(id);
        if (ObjectUtil.isNull(info)) {
            return null;
        }
        Nyt7PlotDetailInfoExVo vo = MapstructUtils.convert(info, Nyt7PlotDetailInfoExVo.class);
        if (vo != null) {
            Nyt7FarmerInsuranceInfo farmerInsuranceInfo = info.getFarmerInsuranceInfo();
            vo.setFarmerInsuranceInfoVo(MapstructUtils.convert(farmerInsuranceInfo, Nyt7FarmerInsuranceInfoVo.class));
        }
        return vo;

    }

    /**
     * 分页查询地块详细信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 地块详细信息分页列表
     */
    @Override
    public TableDataInfo<Nyt7PlotDetailInfoExVo> queryPageList(Nyt7PlotDetailInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = buildQueryWrapper(bo);
        Page<Nyt7PlotDetailInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<Nyt7PlotDetailInfoExVo> exVoPage = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());
        List<Nyt7PlotDetailInfoVo> records = result.getRecords();
        List<Nyt7PlotDetailInfoExVo> exVoList = getNyt7PlotDetailInfoExVos(records);
        exVoPage.setRecords(exVoList);
        return TableDataInfo.build(exVoPage);
    }

    private @NotNull List<Nyt7PlotDetailInfoExVo> getNyt7PlotDetailInfoExVos(List<Nyt7PlotDetailInfoVo> records) {
        List<Nyt7PlotDetailInfoExVo> exVoList = records.stream()
            .map(this::convertToExVo)
            .collect(Collectors.toList());
        for (Nyt7PlotDetailInfoExVo exVo : exVoList){
            Nyt7PlotDetailInfoExVo nyt7PlotDetailInfoExVo = queryByIdEx(exVo.getId());
            if (nyt7PlotDetailInfoExVo != null){
                exVo.setFarmerInsuranceInfoVo(nyt7PlotDetailInfoExVo.getFarmerInsuranceInfoVo());
            }
        }
        return exVoList;
    }

    private Nyt7PlotDetailInfoExVo convertToExVo(Nyt7PlotDetailInfoVo vo) {
        return BeanUtil.copyProperties(vo, Nyt7PlotDetailInfoExVo.class);
    }

    /**
     * 查询符合条件的地块详细信息列表
     *
     * @param bo 查询条件
     * @return 地块详细信息列表
     */
    @Override
    public List<Nyt7PlotDetailInfoVo> queryList(Nyt7PlotDetailInfoBo bo) {
        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }




    //用于统计分析
    @Override
    public List<Nyt7PlotDetailInfoExVo> queryListAll(Nyt7PlotDetailInfoBo bo) {

        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = buildQueryWrapperAll(bo);
        List<Nyt7PlotDetailInfoVo> nyt7PlotDetailInfos = baseMapper.selectVoList(lqw);
        return getNyt7PlotDetailInfoExVos(nyt7PlotDetailInfos);
    }
    private LambdaQueryWrapper<Nyt7PlotDetailInfo> buildQueryWrapperAll(Nyt7PlotDetailInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), Nyt7PlotDetailInfo::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getPlotCode()), Nyt7PlotDetailInfo::getPlotCode, bo.getPlotCode());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Nyt7PlotDetailInfo::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    private LambdaQueryWrapper<Nyt7PlotDetailInfo> buildQueryWrapper(Nyt7PlotDetailInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPlotCode()), Nyt7PlotDetailInfo::getPlotCode, bo.getPlotCode());
        lqw.eq(bo.getInsuredArea() != null, Nyt7PlotDetailInfo::getInsuredArea, bo.getInsuredArea());
        lqw.eq(bo.getMeasuredArea() != null, Nyt7PlotDetailInfo::getMeasuredArea, bo.getMeasuredArea());
        lqw.eq(bo.getFarmerId() != null, Nyt7PlotDetailInfo::getFarmerId, bo.getFarmerId());
        lqw.eq(StringUtils.isNotBlank(bo.getConfirmationStatus()), Nyt7PlotDetailInfo::getConfirmationStatus, bo.getConfirmationStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), Nyt7PlotDetailInfo::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), Nyt7PlotDetailInfo::getLongitude, bo.getLongitude());


//        lqw.eq(StringUtils.isNotBlank(bo.getRegionBoundary()), Nyt7PlotDetailInfo::getRegionBoundary, bo.getRegionBoundary());





        lqw.eq(StringUtils.isNotBlank(bo.getCity()), Nyt7PlotDetailInfo::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrictCounty()), Nyt7PlotDetailInfo::getDistrictCounty, bo.getDistrictCounty());
        lqw.eq(StringUtils.isNotBlank(bo.getTown()), Nyt7PlotDetailInfo::getTown, bo.getTown());
        lqw.eq(StringUtils.isNotBlank(bo.getVillage()), Nyt7PlotDetailInfo::getVillage, bo.getVillage());
        return lqw;
    }





    /**
     * 新增地块详细信息
     *
     * @param bo 地块详细信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7PlotDetailInfoBo bo) {
        Nyt7PlotDetailInfo add = MapstructUtils.convert(bo, Nyt7PlotDetailInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地块详细信息
     *
     * @param bo 地块详细信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7PlotDetailInfoBo bo) {
        Nyt7PlotDetailInfo update = MapstructUtils.convert(bo, Nyt7PlotDetailInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7PlotDetailInfo entity){
        //TODO 做一些数据校验,如唯一约束
//        //检查地块的农户id是否外键关联
//        if (entity.getFarmerId() != null) {
//            Nyt7FarmerInsuranceInfo farmer = Nyt7FarmerInsuranceInfoMapper.selectById(entity.getFarmerId());
//            if (farmer == null) {
//                throw new RuntimeException("Farmer ID does not exist");
//            }
//        }

    }

    /**
     * 校验并批量删除地块详细信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }







    @Override
    public List<Nyt7PlotDetailInfoVo> queryListByIds(List<Long> ids) {
        LambdaQueryWrapper<Nyt7PlotDetailInfo> lqw = Wrappers.lambdaQuery();
        lqw.in(Nyt7PlotDetailInfo::getId, ids);
        return baseMapper.selectVoList(lqw);
    }








}
